/*
 * File:   Network.cpp
 * Author: henrya
 *
 * Created on June 13, 2009, 8:43 PM
 */


#include "IOException.h"
#include "Tokenizer.h"
#include "Node.h"
#include "RuntimeException.h"
#include "Network.h"
#include <cstdlib>
#include <iostream>
using std::cout;

Network::Network() {

}

Network* Network::createNetwork(ifstream& netConfStream) {
    Network* res = new Network();
    cout << "Creating a new network!" << endl;
    string nodeCountString;
    if (getline(netConfStream, nodeCountString) == 0) {
        RuntimeException ex("Network: Unexpected end of file or file format.");
        throw ex;
    }
    Tokenizer toknzr(nodeCountString);
    string* countString = toknzr.next();
    if (countString == NULL) {
        RuntimeException ex("Got NULL string will parsing node count.");
        throw ex;
    }
    res->nodeCount = atoi((*countString).c_str());
    if (res->nodeCount == 0) {
        RuntimeException ex("Node Count must be greater then 0.");
        throw ex;
    }
    string nodeConf;

    for (int nodesRead(0); nodesRead < res->nodeCount && getline(netConfStream, nodeConf) != 0; nodesRead++) {
        Node* newNode = Node::createNode(res, nodeConf);
        res->nodes[newNode->getId()] = newNode;
        res->nodesIds.push_back(newNode->getId());
    }
    return res;
}

Network::~Network() {
}

int Network::getNodesCount() {
    return nodeCount;
}

vector<int>* Network::getNodesIds() {
    return &nodesIds;
}

void Network::printRTs(){
    cout<<"printing network"<<endl;
    map<int, Node*>::iterator iter;
    for( iter = nodes.begin(); iter != nodes.end(); ++iter ) {
        cout<<"printing node "<<iter->first<<"\t";
        (iter->second)->printRT();
    }


}
